<?php
namespace Home\Controller;
use Think\Controller;
class SelfController extends Controller {

    public function index(){
        echo "1";
    }
    /**
     * [duplicating 复印纸排行]
     * @echo [json] [二级分类名称和对应的交易额]
     */
    public function duplicating(){

        $Model = new \Think\Model();
        $categoryData = $Model->query("SELECT id, name FROM tlw_shopcategory WHERE pid in ( SELECT id FROM tlw_shopcategory WHERE name LIKE '复印纸' )");

        foreach ($categoryData as $key => $value) {
            $priceData[] = $Model->query("SELECT SUM( a.payment_price ) as totalprice,  c.`name` FROM tlw_order_sku as a LEFT JOIN tlw_item as b ON a.item_id=b.item_id LEFT JOIN tlw_shopcategory as c ON c.id=b.plat_item_id WHERE b.plat_item_id = ".$value['id']." AND b.shop_id in ( 1002232,1002231,1002233,1002222,1002223,1002234,1002235,1002236,1002226,1002225,1002224,1002227,1002250,1002253,1002252,1002251,1002228,1002229,1002230,1002240,1002239,1002242,1002241,1002243,1002244,1002247,1002246,1002245,100228,1002237,1002248,1002249,1002324 )");

        } 
        
        $priceDatas = array();
        foreach ($priceData as $key => $value) {
            $priceDatas[] = $value[0];
        }

        foreach ($priceDatas as $key => $value) {
            $priceDatas[$key] = array_filter($value);
        }

        foreach ($priceDatas as $key => $value) {
            if (!$value) {
                unset($priceDatas[$key]);
            } 
        }

        $res = array();
        foreach($priceDatas as $item) {
          if(! isset($res[$item['name']])) $res[$item['name']] = $item;
          else $res[$item['name']]['totalprice'] .= ',' . $item['totalprice'];
        }
        // var_export(array_values($res));
        $brandData = array_values($res);

        foreach ($brandData as $key => $value) {
            $prices = explode(',', $value['totalprice']);
            $totalprice = intval(array_sum($prices));
            $brandData[$key]['totalprice'] = $totalprice;
            $brandData[] = $totalprice;
        }

        $a = array();
        foreach($brandData as $key=>$val){
            $a[] = $val['totalprice']; 
        }
        rsort($a);
        $a = array_flip($a);
        $result = array();
        foreach($brandData as $k=>$v){
            $temp1 = $v['totalprice'];
            $temp2 = $a[$temp1];
            $result[$temp2] = $v;
        }
        ksort($result);

        foreach ($result as $key => $value) {
            if (empty($key)) {
                unset($result[$key]);
            }
        }

        $unit = 10000;
        foreach ($result as $key => $value) {
            if ($value['totalprice']>$unit) {
                $result[$key]['totalprice'] = intval($value['totalprice']/$unit);
            }
        }

        $results = array_slice($result,0,5);
        // dump($results);
        
        echo json_encode($results);
    }

    /**
     * [form 复印纸排行]
     * @echo [json] [二级分类名称和对应的交易额]
     */
    public function form(){

        $Model = new \Think\Model();
        $categoryData = $Model->query("SELECT id, name FROM tlw_shopcategory WHERE pid in ( SELECT id FROM tlw_shopcategory WHERE name LIKE '打印纸' )");

        foreach ($categoryData as $key => $value) {
            $priceData[] = $Model->query("SELECT SUM( a.payment_price ) as totalprice,  c.`name` FROM tlw_order_sku as a LEFT JOIN tlw_item as b ON a.item_id=b.item_id LEFT JOIN tlw_shopcategory as c ON c.id=b.plat_item_id WHERE b.plat_item_id = ".$value['id']." AND b.shop_id in ( 1002232,1002231,1002233,1002222,1002223,1002234,1002235,1002236,1002226,1002225,1002224,1002227,1002250,1002253,1002252,1002251,1002228,1002229,1002230,1002240,1002239,1002242,1002241,1002243,1002244,1002247,1002246,1002245,100228,1002237,1002248,1002249,1002324 )");

        } 
        
        $priceDatas = array();
        foreach ($priceData as $key => $value) {
            $priceDatas[] = $value[0];
        }

        foreach ($priceDatas as $key => $value) {
            $priceDatas[$key] = array_filter($value);
        }

        foreach ($priceDatas as $key => $value) {
            if (!$value) {
                unset($priceDatas[$key]);
            } 
        }

        $res = array();
        foreach($priceDatas as $item) {
          if(! isset($res[$item['name']])) $res[$item['name']] = $item;
          else $res[$item['name']]['totalprice'] .= ',' . $item['totalprice'];
        }
        // var_export(array_values($res));
        $brandData = array_values($res);

        foreach ($brandData as $key => $value) {
            $prices = explode(',', $value['totalprice']);
            $totalprice = intval(array_sum($prices));
            $brandData[$key]['totalprice'] = $totalprice;
            $brandData[] = $totalprice;
        }

        $a = array();
        foreach($brandData as $key=>$val){
            $a[] = $val['totalprice']; 
        }
        rsort($a);
        $a = array_flip($a);
        $result = array();
        foreach($brandData as $k=>$v){
            $temp1 = $v['totalprice'];
            $temp2 = $a[$temp1];
            $result[$temp2] = $v;
        }
        ksort($result);

        foreach ($result as $key => $value) {
            if (empty($key)) {
                unset($result[$key]);
            }
        }

        $unit = 10000;
        foreach ($result as $key => $value) {
            if ($value['totalprice']>$unit) {
                $result[$key]['totalprice'] = intval($value['totalprice']/$unit);
            }
        }

        $results = array_slice($result,0,5);
        // dump($results);
        
        echo json_encode($results);
    }

    
}